<!-- 登录 login 页面 -->
<template>
	<view class="login-dom">
		<view class="login-top">
			<image src="@/static/images/login-bg.png" style="width: 100%;"></image>
		</view>
		<view class="login-main">
			<view class="login-name">登 录</view>
			<uni-forms ref="formRef" :model="formData" :rules="formRules" class="p10 forms-dom">
				<uni-forms-item name="account">
					<uni-easyinput prefixIcon="person-filled" v-model="formData.account" placeholder="请输入学号/手机号/身份证" trim/>
				</uni-forms-item>
				<uni-forms-item name="name2">
					<uni-easyinput prefixIcon="locked-filled" v-model="formData.password" type="password" placeholder="请输入密码" trim/>
				</uni-forms-item>
				<uni-forms-item name="userType">
					<view style="float: right;">
						<uni-data-checkbox mode="button" v-model="formData.userType" :localdata="range" selectedColor="#01c2b7"></uni-data-checkbox>
					</view>
				</uni-forms-item>
			</uni-forms>
			<button @click="loginOk" type="default" class="basicbutton m10" hover-class="basicbutton-hover">登录</button>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { sm2doEncrypt } from "@/utils/smCrypto"
	import { setToken, setUser,  setDictType } from "@/utils/storage"
	import { doLogin } from "@/api/user"
	import { ref } from 'vue'
	import { onShow } from '@dcloudio/uni-app'

	const range = ref([
		{ value: 1, text: "学生" },
		{ value: 2, text: "专家" },
	])
	const formData = ref({
		account: '21884708',
		password: '041537',
		userType: null
	})
	// 学生：21884708/041537  2018881807 163736
	// 专家: 13887650234/123456
	const formRules = ref({
		account: {
			rules: [{ required: true, errorMessage: '请输入学号/手机号/身份证' }],
		},
		password: {
			rules: [{ required: true, errorMessage: '请输入密码' }],
		},
		userType: {
			rules: [{ required: true, errorMessage: '请选择用户角色' }],
		},
	})
	let formRef = ref(null)

	onShow(() => {
		let userTypeStorage = uni.getStorageSync('userType')
		let userAccount = uni.getStorageSync('userAccount')
		let userPassword = uni.getStorageSync('userPassword')
		formData.value.account = userAccount || ''
		formData.value.password = userPassword || ''
		formData.value.userType = userTypeStorage || 1
	})

	function loginOk() {
		formRef.value.validate().then(() => {
			doLogin({
				userType: formData.value.userType,
				account: formData.value.account,
				password: sm2doEncrypt(formData.value.password)
			}).then(res => {
				if (res.code == '200') {
					getPageNum(res.data)
				}
			})
		}).catch(err => {
			uni.showToast({ icon: "none", title: err[0].errorMessage })
		})
	}
	// 登录成功跳转
	function getPageNum(data) {
		let userType = formData.value.userType
		let pages = getCurrentPages() //获取加载的页面
		let currentPage = pages[pages.length - 2];   // 重新登录
		if (currentPage) {
			setToken(data.sessionToken)
			setUser(data)
			uni.navigateBack({
				delta: 1
			})
		} else {
			uni.clearStorage(); // 清除缓存
			if (userType == 1) {
				setcache(data, 1)
				uni.reLaunch({
					url: '/pages/tabBar/student/recruit'
				});
			} else {
				setcache(data, 2)
				uni.reLaunch({
					url: '/pages/tabBar/teacher/consult'
				});
			}
		}
	}
	// 登录缓存
	function setcache(data, userType) {
		setToken(data.sessionToken)
		setUser(data)
		setDictType()
		uni.setStorageSync('userAccount', formData.value.account)
		uni.setStorageSync('userPassword', formData.value.password)
		uni.setStorageSync('userType', userType);
	}
</script>

<style lang="scss" scoped>
	.login-dom {
		min-height: 100vh;
		background-color: #00b9fe;
	}

	.login-img {
		width: 56px;
		height: 56px;
		border-radius: 52%;
		background-color: #fff;
		border: 1px solid #DCDCDC;
		box-shadow: rgba(0, 0, 0, 0.08) 0px 0px 3px 1px;
	}

	.login-main {
		margin: 10px 20px 50px;
		padding: 10px 20px 30px;
		background-color: rgba(255, 255, 255, 1);
		border-radius: 8px;

	}

	.login-name {
		padding: 10px 0 20px;
		font-size: 40rpx;
		font-weight: 600;
		color: #01c2b7;
		text-align: center;
	}
</style>